/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.mall.common.utils;

import com.mall.common.constant.ResultCode;
import org.apache.commons.lang3.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 *
 * @author Mark sunlightcs@gmail.com
 */
public class R extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public R() {
        put("code", ResultCode.SUCCESS.getCode());
        put("message", "操作成功");
    }

    public static R error() {
        return error(ResultCode.FAIL.getCode(),"请求失败");
    }

    public static R error(String message) {
        return error(ResultCode.FAIL.getCode(), message);
    }

    public static R error(int code, String message) {
        R r = new R();
        r.put("code", code);
        r.put("message", message);
        return r;
    }

    public static R ok(String message) {
        R r = new R();
        r.put("message", message);
        return r;
    }

    public static R okdata(Map<String, Object> map) {
        R r = new R();
        if(map.containsKey("total")
                && StringStrUtils.isNotBlank(map.get("total").toString())){
            r.put("total",map.get("total"));
            map.remove("total");
        }
        r.put("data",map);
        return r;
    }
    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }
    public static R ok(Object data) {
        R r = new R();
        r.put("data",data);
        return r;
    }
    public static R page(PageUtils pageUtils) {
        R r = new R();
        r.put("data",pageUtils.getRows());
        r.put("total",pageUtils.getTotal());
        return r;
    }
    public static R ok() {
        return new R();
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
